package cn.chinasyq.photoquan.news.mode;

import android.content.Context;

import java.util.ArrayList;
import java.util.HashMap;

import cn.chinasyq.photoquan.NetworkConstant;
import cn.chinasyq.photoquan.ParamConstant;
import cn.chinasyq.photoquan.news.bean.ArticlesInfoEntity;
import cn.chinasyq.photoquan.news.bean.ArticlesInfoPojo;
import cn.chinasyq.photoquan.news.bean.ArticlesList;
import cn.chinasyq.photoquan.news.bean.ArticlesListPojo;
import cn.chinasyq.photoquan.news.bean.Favorite;
import cn.chinasyq.photoquan.news.bean.FavoritePojo;
import cn.chinasyq.photoquan.photo.bean.GameEntity;
import cn.chinasyq.photoquan.photo.bean.GamesPojo;
import cn.chinasyq.photoquan.user.bean.Album;
import cn.chinasyq.photoquan.user.bean.AlbumsPojo;
import cn.chinasyq.photoquan.user.bean.GameOrder;
import cn.chinasyq.photoquan.user.bean.GameOrdersPojo;
import cn.chinasyq.photoquan.user.mode.UserMode;
import cn.master.volley.commons.CacheHelper;
import cn.master.volley.commons.VolleyHelper;
import cn.master.volley.models.response.handler.ResponseHandler;
import cn.master.volley.models.response.listener.ResolveJsonHelper;
import cn.master.volley.models.response.listener.ResponseListener;

/**
 * Created by 一搏 on 2015/11/24.
 */
public class NewsMode {
    private static int page;
    public static final int MODE_REFRESH = 1;
    public static final int MODE_LOAD_MORE = 2;

    /**
     * 资讯列表banner+list
     *
     * @param handler
     * @param mode    obtain data mode
     */
    public static void obtainActiclesList(ResponseHandler handler, int mode) {
        switch (mode) {
            case MODE_LOAD_MORE:
                page++;
                break;
            case MODE_REFRESH:
                page = 1;
                break;
            default:
                break;
        }
        String url = String.format(NetworkConstant.API_ARTICLE_LIST, page);
        VolleyHelper.invalidateCache(url);
        ResponseListener<ArticlesList> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(ArticlesListPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    /**
     * 资讯列表banner+list
     *
     * @param handler
     */
    public static void obtainGameList(ResponseHandler handler) {
        String url = NetworkConstant.API_GAMES;
        VolleyHelper.invalidateCache(url);
        ResponseListener<ArrayList<GameEntity>> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(GamesPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static ArrayList<GameEntity> getGamesByCache() {
        String url = NetworkConstant.API_GAMES;
        return CacheHelper.getCache(url, ResolveJsonHelper.resolveJson2POJO(GamesPojo.class));
    }
    public static void obtainMyGames(ResponseHandler handler, int mode, String token) {
        switch (mode) {
            case MODE_LOAD_MORE:
                page++;
                break;
            case MODE_REFRESH:
                page = 1;
                break;
            default:
                break;
        }
        String url = String.format(NetworkConstant.API_MY_GAMES, page, token);
        VolleyHelper.invalidateCache(url);
        ResponseListener<ArrayList<GameOrder>> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(GameOrdersPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static ArrayList<GameOrder> getGameOrdersByCache(String token) {
        String url = String.format(NetworkConstant.API_MY_GAMES, 1, token);
        return CacheHelper.getCache(url, ResolveJsonHelper.resolveJson2POJO(GameOrdersPojo.class));
    }
    /**
     * 资讯列表banner+list
     *
     * @param handler
     * @param mode    obtain data mode
     * @param token
     */
    public static void obtainMyAlbum(ResponseHandler handler, int mode, String token) {
        switch (mode) {
            case MODE_LOAD_MORE:
                page++;
                break;
            case MODE_REFRESH:
                page = 1;
                break;
            default:
                break;
        }
        String url = String.format(NetworkConstant.API_MY_ALBUM, page, token);
        VolleyHelper.invalidateCache(url);
        ResponseListener<ArrayList<Album>> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(AlbumsPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static ArrayList<Album> getAlbumsByCache(String token) {
        String url = String.format(NetworkConstant.API_MY_ALBUM, 1, token);
        return CacheHelper.getCache(url, ResolveJsonHelper.resolveJson2POJO(AlbumsPojo.class));
    }

    public static void searchActiclesList(ResponseHandler handler, String keyword) {
        String url = String.format(NetworkConstant.API_ARTICLE_SEARCH, keyword);
        VolleyHelper.invalidateCache(url);
        ResponseListener<ArticlesList> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(ArticlesListPojo.class));
        VolleyHelper.get(url, true, listener, listener);
    }

    public static ArticlesList getActiclesListByCache() {
        String url = String.format(NetworkConstant.API_ARTICLE_LIST, 1);
        return CacheHelper.getCache(url, ResolveJsonHelper.resolveJson2POJO(ArticlesListPojo.class));
    }

    public static void obtainArticleInfo(Context c, String uuid,
                                         ResponseHandler handler) {
        String url = String.format(NetworkConstant.API_ARTICLE_INFO, uuid, UserMode.getCurrentUserInfo(c).getUuid());
        ResponseListener<ArticlesInfoEntity> listener = new ResponseListener<>(
                handler,
                ResolveJsonHelper.resolveJson2POJO(ArticlesInfoPojo.class));
        VolleyHelper.invalidateCache(url);
        VolleyHelper.get(url, true, listener, listener);
    }

    public static void applaudQuestion(Context c, String article_uuid, ResponseHandler handler) {
        String url = NetworkConstant.API_ARTICLE_FAVORITE;
        ResponseListener<Favorite> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2POJO(FavoritePojo.class));
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken() + "");
        params.put(ParamConstant.POST_PARAMS_USER_UUID, UserMode.getCurrentUserInfo(c).getUuid() + "");
        params.put(ParamConstant.POST_PARAMS_UUID, article_uuid);
        VolleyHelper.post(url, params, listener, listener);
    }

    public static void upComment(Context c, String uuid, ResponseHandler handler) {
        String url = NetworkConstant.API_ARTICLE_UP;
        ResponseListener<String> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveJson2String);
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken() + "");
        params.put(ParamConstant.POST_PARAMS_USER_UUID, UserMode.getCurrentUserInfo(c).getUuid() + "");
        params.put(ParamConstant.POST_PARAMS_UUID, uuid);
        VolleyHelper.post(url, params, listener, listener);
    }

    public static void sendComment(Context c, String article_uuid, String comment, ResponseHandler handler) {
        String url = NetworkConstant.API_ARTICLE_FAVORITE;
        ResponseListener<Object> listener = new ResponseListener<>(
                handler, ResolveJsonHelper.resolveAndIgnoreData);
        HashMap<String, String> params = new HashMap<>();
        params.put(ParamConstant.POST_PARAMS_TOKEY, UserMode.getCurrentUserInfo(c).getToken() + "");
        params.put(ParamConstant.POST_PARAMS_USER_UUID, UserMode.getCurrentUserInfo(c).getUuid() + "");
        params.put(ParamConstant.POST_PARAMS_UUID, article_uuid);
        params.put(ParamConstant.POST_PARAMS_COMMENTS, comment);
        VolleyHelper.post(url, params, listener, listener);
    }
}
